package com.led_on_off.led.slice;

import com.led_on_off.led.ListProvider;
import com.led_on_off.led.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bluetooth.BluetoothDeviceClass;
import ohos.bluetooth.BluetoothHost;
import ohos.bluetooth.BluetoothRemoteDevice;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class DeviceListSlice extends AbilitySlice {
    public static String EXTRA_ADDRESS = "device_address";

    ListContainer devicelist;
    Button button;
    BluetoothHost myBlueTooth = null;
    ToastDialog toastDialog = null;
    private List<BluetoothRemoteDevice> pairedDevices;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_device_list);

        button = (Button) findComponentById(ResourceTable.Id_button);
        devicelist = (ListContainer) findComponentById(ResourceTable.Id_listView);

        myBlueTooth = BluetoothHost.getDefaultHost(getContext());

        toastDialog = new ToastDialog(getContext());

        if (myBlueTooth == null) {

            //HiLog.debug(new HiLogLabel(HiLog.LOG_APP, 0x00201, "blueTooth connected"),null);
            toastDialog.setText("Bluetooth Device Not Available").show();
            //terminate();
        } else if (!myBlueTooth.enableBt()) {
            //HiLog.debug(new HiLogLabel(HiLog.LOG_APP, 0x00201, "blueTooth unconnected"),null);

            Intent intent1 = new Intent();
            intent1.setParam("1", BluetoothHost.STATE_TURNING_ON);
            startAbilityForResult(intent1, 1);
        }

        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                pairedDevicesList();
            }
        });

    }

    private void pairedDevicesList() {
        pairedDevices = myBlueTooth.getPairedDevices();
        ArrayList list = new ArrayList();
        if (pairedDevices.size() > 0) {
            for (BluetoothRemoteDevice bt : pairedDevices) {
                list.add(bt.getDeviceName().get() + bt.getDeviceAddr()); //Get the device's name and the address
            }
        } else {
            toastDialog.setText("No Paired Bluetooth Devices Found.").show();
        }

        final ListProvider listProvider = new ListProvider(this, list);
        devicelist.setItemProvider(listProvider);
        devicelist.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.led_on_off.led")
                        .withAbilityName("com.led_on_off.led.LedControl")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
